package it.polimi.traveldream.web.beans.employee;

import it.polimi.traveldream.ejbclient.dtos.TransportDTO;
import it.polimi.traveldream.ejbclient.eaos.BaseProductServiceEAO;
import it.polimi.traveldream.utils.exceptions.NotCoherentException;
import it.polimi.traveldream.web.beans.shared.FacesContextHandler;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="addTransport")
@RequestScoped
public class AddTransportBean {
	@EJB
	private BaseProductServiceEAO bService;
	private TransportDTO transport;
	
	public AddTransportBean() {
	}
	
	@PostConstruct
	public void init() {
		transport = new TransportDTO();
		transport.setMaxAvailability(1);
	}
	
	public TransportDTO getTransport() {
		return transport;
	}

	public void setTransport(TransportDTO transport) {
		this.transport = transport;
	}

	public String addTransport() {
		try {
			bService.addProduct(transport);
			return "index";
		}  catch (NotCoherentException e) {
			FacesContextHandler.produceFacesMessage(null,FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The composition of the product is not coherent. Please check its fields.");
			return "addTransport";
		}
	}
	
}
